﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Easy.Operation
{
    public class BusinessUnitClass
    {
        public string BusinessUnitId { get; set; }
        public string Name { get; set; }
        public string PreBusinessUnit { get; set; }

        public string PreBusinessUintName { get; set; }
    }

    public class ResultClass
    {
        public int Result { get; set; }
        public string Remark {get;set; }
    }

    public class SystemUserClass
    {
        public string UserId { get; set; }
        public string BusinessUnit{ get; set; }
        public string Name { get; set; }
        public string BusinessUnitName { get; set; }
    }

    public class RoleClass
    {
        public string RoleId { get; set; }
        public string Name { get; set; }
        public string BusinessUnit { get; set; }
        public string OwningBusinessUnit { get; set; }
    }

    public class UserRoleClass
    {
        public string UserId { get; set; }
        public List<RoleClass> Roles { get; set; }
    }

    public class EntityPrivilegeClass
    {
        public string EntityId { get; set; }
        public string EntityName { get; set; }
        public string EntityDisplayName { get; set; }
        public int Read { get; set; }
        public int Write { get; set; }
        public int Create { get; set; }
        public int Delete { get; set; }
        public int Assign { get; set; }
    }

    public class RolePrivilegeClass
    {
        public string RoleId { get; set; }
        public List<EntityPrivilegeClass> EntityPrivileges { get; set; }
    }

    //
    public class ChildSiteMapClass
    {
        public string ChildSiteMapId { get; set; }
        public string ChildName { get; set; }
        public string ChildUrl { get; set; }
        public string ChildIcon { get; set; }
        public string ChildPrivilege { get; set; }
    }

    public class SiteMapClass
    {
        public string SiteMapId { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string Icon { get; set; }
        public string Privilege { get; set; }
        public List<ChildSiteMapClass> ChildSiteMap { get; set; }
    }
    //

    public static class JsonHelper
    {
        //扩展方法
        public static string ToJson(this object obj)
        {
            //JSON序列化
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            //定义一个stream用来存放序列化之后的内容
            Stream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            //从头到尾将stream读取成一个字符串形式的数据，并且返回
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }



        /// <summary>

        /// JSON序列化

        /// </summary>
        public static string JsonSerializer<T>(T t)
        {

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

            MemoryStream ms = new MemoryStream();

            ser.WriteObject(ms, t);

            string jsonString = System.Text.Encoding.UTF8.GetString(ms.ToArray());

            ms.Close();

            return jsonString;

        }



        /// <summary>

        /// JSON反序列化

        /// </summary>

        public static T JsonDeserialize<T>(string jsonString)
        {

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

            MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonString));

            T obj = (T)ser.ReadObject(ms);

            return obj;

        }


        /// <summary>  
        /// dataTable转换成Json格式  
        /// </summary>  
        /// <param name="dt"></param>  
        /// <returns></returns>  
        public static string DataTable2Json(System.Data.DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{\"Name\":\"" + dt.TableName + "\",\"Rows");
            jsonBuilder.Append("\":[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString().Replace("\"", "\\\"")); //对于特殊字符，还应该进行特别的处理。
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }

        /// <summary>  
        /// DataSet转换成Json格式  
        /// </summary>  
        /// <param name="ds">DataSet</param>  
        /// <returns></returns>  
        public static string Dataset2Json(System.Data.DataSet ds)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{\"Tables\":");
            json.Append("[");
            foreach (System.Data.DataTable dt in ds.Tables)
            {
                json.Append(DataTable2Json(dt));
                json.Append(",");
            }
            json.Remove(json.Length - 1, 1);
            json.Append("]");
            json.Append("}");
            return json.ToString();
        }




    }
}